package com.bjsxt.userscenter.head.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.userscenter.common.controller.BaseController;
import com.bjsxt.userscenter.common.util.ConstatFinalUtil;
import com.bjsxt.userscenter.common.util.EncryptUtil;
import com.bjsxt.userscenter.users.pojo.AUsers;
import com.bjsxt.userscenter.users.service.IUsersService;
/**
 * 用户登录后的页面
 * @author FanXiaokang
 *
 */

@Controller
@RequestMapping("/head/users")
public class UserHeadController extends BaseController
{
	@Resource
	private IUsersService usersService;
	
	/**
	 * 用户修改页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/usersUpdate")
	public String usersUpdate(HttpServletRequest request)
	{
		
		ConstatFinalUtil.LOGGER.info("---usersUpdate----");
		HttpSession session = request.getSession();
		AUsers users = (AUsers) session.getAttribute("users");
		/*根据id查用户*/
		Map<String,Object> condMap = new HashMap<String, Object>();
		condMap.put("id", users.getId());
		condMap.put("extend", "true");
		users = this.usersService.findOneUsersService(condMap);
		request.setAttribute("users", users);
		return "/head/users/usersUpdate";
	}
	
	@RequestMapping("/usersUpdateSubmit")
	public String usersUpdateSubmit(HttpServletRequest request)
	{
		ConstatFinalUtil.LOGGER.info("====usersUpdateSubmit====");
		HttpSession session = request.getSession();
		AUsers users = (AUsers) session.getAttribute("users");
		/*根据id查用户*/
		Map<String,Object> condMap = new HashMap<String, Object>();
		condMap.put("id", users.getId());
		users = this.usersService.findOneUsersService(condMap);
		/**
		 * 操 作 标识 flag
		 */
		String operType = request.getParameter("operType");
		
		/*
		 * 断言
		 */
		boolean updateFlag = false;
		if("updateInfo".equalsIgnoreCase(operType))
		{
			/**
			 * 更新个人资料
			 */
			String qq = request.getParameter("qq");
			String address = request.getParameter("address");
			String regionId = request.getParameter("regionId");
			users.setRegionId(Integer.valueOf(regionId));
			users.setQq(qq);
			users.setAddress(address);
			
			updateFlag = true;
 		}else if("updatePass".equalsIgnoreCase(operType))
 		{
 			/* 修改密码*/
 			String souPass = request.getParameter("souPass");
 			String password = request.getParameter("password");
 			if(EncryptUtil.checkPass(souPass, users.getPassword()))
 			{
 				users.setPassword(EncryptUtil.encodeStr(password));
 				updateFlag = true;
 			}else
 			{
 				//原密码不正确
 				this.info = ConstatFinalUtil.INFO_JSON.getString("17");
 			}
 		}else
 		{
 			this.info = ConstatFinalUtil.INFO_JSON.getString("18");
 		}
		if(updateFlag)
		{
			users.setUpdateTime(new Date());
			JSONObject resDbJSON = this.usersService.updateOneUsersService(users);
			this.info = resDbJSON.getString("info");
		}
		request.setAttribute("info", info);
		return this.usersUpdate(request) ; 
	}
	
	
	@RequestMapping("/main")
	public String main()
	{
		ConstatFinalUtil.LOGGER.info("----main----");
		return "/head/users/main";
	}
	
	
	
	/**
	 * 退出
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request,HttpSession session)
	{
		ConstatFinalUtil.LOGGER.info("-- logout --");
		session.removeAttribute("users");
		session.removeAttribute("lastLoginTime");
		//操作token 
		String token = request.getParameter("token");
		if(token==null)
		{
			token="";
		}
		if(!"".equalsIgnoreCase(token))
		{
			ConstatFinalUtil.TOKENS_MAP.remove(token);
		}
		this.info = ConstatFinalUtil.INFO_JSON.getString("8");
		/*request.setAttribute("info", info);
		return "/back/login" ; 
		*/
		session.setAttribute("info", info);
		return "redirect:/login.htm";
	}
}
